#============================================================
#	Type Layer
#============================================================
# @datetime: 2022-4-19 23:58:09
#============================================================
class_name PlayerTypeLayer
extends Node2D


onready var canvas : PlayerCanvas = $Canvas
onready var controller : PlayerController = $Controller
onready var collision : PlayerCollision = $Collision
onready var statemachine : PlayerStateMachineRoot = $PlayerStateMachineRoot

# [ Wrapper ]
onready var equipment_wrapper : EquipmentWrapper = WrapperHelper.get_equipment(owner)
onready var attack_wrapper : AttackWrapper = WrapperHelper.get_attack(owner)
onready var property_wrapper : PropertyWrapper = WrapperHelper.get_property(owner)


#============================================================
#   Set/Get
#============================================================
func get_host() -> KinematicBody2D:
	return owner as KinematicBody2D


#============================================================
#   内置
#============================================================
func _ready():
	# 攻击
	WrapperHelper.get_attack(owner) \
		.wrapper_connect("attacked", self, "_attacked")
	
	# 属性
	WrapperHelper.get_property(owner) \
		.wrapper_connect("property_changed", self, "_property_change") \
		.emit_property_changed_signal()
	
	# 伤害
	WrapperHelper.get_damage(owner) \
		.connect("took_damage", self, "_take_damage")
	
	# 装备
	WrapperHelper.get_equipment(owner) \
		.wrapper_connect("loaded", self, "_loaded") \
		.wrapper_connect("unloaded", self, "_unloaded")
	
	# 碰撞体
	WrapperHelper.get_collision(owner) \
		.connect("disabled_changed", self, "_disabled_changed")


#============================================================
#   连接信号
#============================================================
# 发出攻击
func _attacked(data):
#	Logger.info(self, ['发出攻击：', data])
	var weapon_data = equipment_wrapper.get_type_first_item(ItemBean.Type.WEAPON)
	if weapon_data.size() == 0:
		return
	
	# 当前武器数据
	var weapon : WeaponBean = JsonUtil.dict2object(weapon_data, WeaponBean)
	match weapon.weapon_type:
		
		# 近战武器
		WeaponBean.WeaponType.SHORT:
			canvas._attack_effect.frame = 0
			collision.set_attack_collision_disabled(false)
		
		# 远程武器
		WeaponBean.WeaponType.LONG:
			var arrow = preload("res://src/scene/weapon/Arrow.tscn").instance()
			var dir : Vector2 = controller.move_controller.get_direction()
			arrow.set_host(owner)
			arrow.set_global_position(get_host().global_position)
			arrow.set_global_rotation(0 if dir.x == -1 else PI)
			get_tree().current_scene.call_deferred("add_child", arrow)

# 对目标成伤害
func _to_damage(target):
	collision.set_attack_collision_disabled(true)
	AttackHelper.attack_to(owner, target)

##  属性改变 
## @_property  
## @value  
func _property_change(_property, value):
	if _property in controller:
		controller.set(_property, value)

##  装备物品
## @data  
func _loaded(data: Dictionary):
	var item : WeaponBean = JsonUtil.dict2object(data, WeaponBean)
	if item.type == ItemBean.Type.WEAPON:
		attack_wrapper.set_interval(item.attack_speed)

##  卸载物品 
## @data  
func _unloaded(data: Dictionary):
	attack_wrapper.set_interval(0)

##  受到伤害
## @data  
func _take_damage(data: Dictionary):
	property_wrapper.sub_property("health", data['damage'])

# 转换方向
func _face_direction_changed(direction: Vector2):
	if direction.x != 0:
		canvas.flip_h(direction.x < 0)

# CollisionWrapper 状态发生改变时
func _disabled_changed(disabled: bool):
	if disabled:
		# 失效时
		PhysicsLayer.sub_mask(get_host(), PhysicsLayer.Layer.Bridge)
		PhysicsLayer.sub_mask(get_host(), PhysicsLayer.Layer.Stairs)
	else:
		PhysicsLayer.add_mask(get_host(), PhysicsLayer.Layer.Bridge)


onready var custom_signal_wrapper : CustomSignalWrapper = WrapperHelper.get_custom_signal(owner)

func _detect_item(item_node):
	if item_node is BaseItem:
		custom_signal_wrapper.emit_custom_signal(CustomSignal.DetectItem, item_node)

func _exit_item(item_node):
	custom_signal_wrapper.emit_custom_signal(CustomSignal.ExitItem, item_node)


func _on_InputKey_inputted():
	var spring = get_node("../Spring")
	spring.start(get_global_mouse_position())


func _on_Spring_arrived():
	Logger.info(self, ["到达位置"])
	pass # Replace with function body.
